// 显示注册弹窗
function showRegisterModal() {
    hideLoginModal();
    $.post(FromUrl,{type:'register',gid:gid},function(html) {
        document.getElementById('floatBall').insertAdjacentHTML('afterend', html);
    });
}

// 删除注册弹窗
function hideRegisterModal() {
    document.querySelector('.register-modal').remove();
    showLoginModal();
}

// 刷新注册页面的图片验证码
function refreshRegisterCaptcha() {
    const captchaImg = document.querySelector('#registerModal .captcha-img');
    captchaImg.src = '/captcha?' + new Date().getTime();
}

// 注册功能
function register() {
    const username = document.getElementById('registerUsername').value;
    const password = document.getElementById('registerPassword').value;
    const confirmPassword = document.getElementById('confirmPassword').value;
    const imageCaptcha = document.getElementById('registerImageCaptcha').value;
    const registerType = document.getElementById('registerType').value;


    // 表单验证
    if (!username || !password || !confirmPassword || !imageCaptcha) {
        layer.msg('请填写完整信息');
        return;
    }

    if (password !== confirmPassword) {
        alert('两次输入的密码不一致');
        return;
    }
    console.log(username,password,confirmPassword,imageCaptcha,gid,cid,registerUrl);
    const data = {
        account:username,
        password:password,
        verify:imageCaptcha,
        type:registerType,
        gid:gid,
        cid:cid
    };
    // 这里添加注册API调用
    $.post(registerUrl,data,function(info){
        if(info.code!=1){
            layer.msg(info.msg,{time:2000});
            refreshRegisterCaptcha();
            return false;
        }else{
            layer.msg(info.msg,{time:2000});
            // 注册成功后关闭注册弹窗，显示登录弹窗
            hideRegisterModal();
        }
    })
}